//
//  JKLoginViewController.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/13.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKLoginViewController: JKBaseViewController {

    /** show */
    public class func show() {
        
        let vc = JKLoginViewController()
        let nav = JKMainNavigationController(rootViewController: vc)
        
        UIApplication.shared.delegate?.window!?.rootViewController?.present(nav, animated: true, completion: nil)
    }
    
    @IBOutlet weak var userAccountField: UITextField!
    @IBOutlet weak var pwdField: UITextField!
    @IBOutlet weak var loginBtn: UIButton!
    
    override func navBackButtonClick() {
        
        dismiss(animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navTitle = "登录"
        
        view.bringSubview(toFront: navBgView)
    }

    @IBAction func loginBtnClick(_ sender: Any) {
        
        if !userAccountField.hasText || !pwdField.hasText {
            
            JKProgressHUD.showError(withStatus: "请输入帐号/密码")
            
            return
        }
        
        JKAccountManager.loginWithUserName(userName: userAccountField.text!, password: pwdField.text!) { (error) in
            
            if error != nil {
                
                JKProgressHUD.showError(withStatus: "帐号/密码错误")
                
                return
            }
            
            dismiss(animated: true, completion: {
                
                JKProgressHUD.showSuccess(withStatus: "登录成功！")
            })
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        JKHideKeyBoard()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
